package com.animal.service.impl;

import com.animal.entity.AnAnimal;
import com.animal.entity.AnCar;
import com.animal.entity.AnUser;
import com.animal.mapper.AnAnimalMapper;
import com.animal.mapper.AnUserMapper;
import com.animal.service.AnAnimalService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author animal
 * @since 2022-01-28
 */
@Service
public class AnAnimalServiceImpl extends ServiceImpl<AnAnimalMapper, AnAnimal> implements AnAnimalService {

    @Autowired
    private AnAnimalMapper anAnimalMapper;
    @Autowired
    private AnUserMapper anUserMapper;
    @Override
    public List<AnAnimal> findAll(int size,int current) {
        Page<AnAnimal> userPages = new Page<>(size,current);
        Page<AnAnimal> userPagess=  anAnimalMapper.selectPage(userPages,null);
        List<AnAnimal> list=userPagess.getRecords();

        for(AnAnimal a:list){
            if(a.getAdoptId()>0){
                a.setAnUser(anUserMapper.selectById(a.getAdoptId()));
            }else{
                a.setAnUser(anUserMapper.selectById(a.getDonationId()));
            }

        }
        return list;
    }

    @Override
    public List<AnAnimal> findByUserId(int current, int size, int userId) {
        Page<AnAnimal> userPages = new Page<>(size,current);
        QueryWrapper<AnAnimal> wrapper = new QueryWrapper<>();
        wrapper.eq("adopt_id",userId);
        Page<AnAnimal> userPagess=  anAnimalMapper.selectPage(userPages,null);
        List<AnAnimal> list=userPagess.getRecords();

        for(AnAnimal a:list){
            if(a.getAdoptId()>0){
                a.setAnUser(anUserMapper.selectById(a.getAdoptId()));
            }else{
                a.setAnUser(anUserMapper.selectById(a.getDonationId()));
            }

        }
        return list;
    }
}
